package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.OrderItem;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.OrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class OrderItemService implements BaseService<OrderItem> {

    @Resource
    private OrderItemMapper orderItemMapper;

    /**
     * @ClassName
     * @Description : 查询订单详情列表
     * @param :
     * @Return :
     * @Author : 汪雄
     * @Date :
    */
    public Rsp searchOrderItemList(String id){
        List<OrderItem> orderItems = orderItemMapper.searchOrderItemList(id);
        if(!StringUtils.isEmpty(orderItems)){
            return Rsp.success(orderItems);
        }
            return null;
    }

    public Rsp deleted(String id){
        orderItemMapper.deleteOneTrue(id);
        return Rsp.success("删除成功");
    }


    @Override
    public PageInfo<OrderItem> searchSomeByPage(PageParam<OrderItem> pageParam) {

        return null;
    }

    @Override
    public OrderItem getOneById(String id) {
        OrderItem oneById = orderItemMapper.getOneById(id);
        return oneById;
    }


    @Override
    public Rsp addOne(OrderItem orderItem) {
        orderItem.setId(UUID.randomUUID().toString().replace("-",""));
        int i = orderItemMapper.addOne(orderItem);
        if(i>0){
            return Rsp.success(orderItem);
        }

        return Rsp.fail();
    }

    @Override
    public Rsp update(OrderItem orderItem) {
        int i = orderItemMapper.update(orderItem);
        if(i>0){
            return Rsp.success(orderItemMapper.getOneById(orderItem.getId()));
        }

        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(OrderItem orderItem) {
        return null;
    }

    public Rsp addOrderItemList(List<OrderItem> orderItems) {
        int i = orderItemMapper.addOrderItemList(orderItems);
        if(i>0){
            String orderNo = null;
            for(OrderItem oi:orderItems){
                orderNo = oi.getOrderNo();
            }
            return Rsp.success(orderItemMapper.searchOrderItemList(orderNo));
        }
        return Rsp.fail();
    }
}
